package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.EditAddressFragment;

public class EditAddressActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout moren;
    private View moren_enter;

    private String name;
    private String phone;
    private String address;
    private Address info;
    private Bundle extras;
    private EditAddressFragment fragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_address);

        initFragment();
        initDatas();
        initViews();
    }

    private void initFragment() {
        fragment = new EditAddressFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_edit, fragment);
        transaction.commit();
    }

    private void initDatas() {
        Intent intent = getIntent();
        extras = intent.getExtras();
        info = (Address) extras.getSerializable("address");

        sendDatas();
    }

    private void sendDatas() {
        fragment.setArguments(extras);

    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("编辑地址");
        TextView enter = (TextView) findViewById(R.id.head_right_text);
        enter.setText("完成");
        enter.setVisibility(View.VISIBLE);
        enter.setOnClickListener(this);



        findViewById(R.id.edit_address_delete).setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.head_back:
                finish();
                break;
            case R.id.head_right_text:
                fragment.updateAddress();
//                setResult(1);
//                finish();
                break;

            case R.id.edit_address_delete:
//                new AlertDialog.Builder(EditAddressActivity.this)
//                        .setMessage("确定要删除吗？")
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                boolean b = BaseApplication.dbController.deleteAddress(info.getId());
//                            }
//                        }).show();
                BaseApplication.dbAddressController.deleteAddress(info.getId());
                setResult(1);
                finish();
                break;

        }
    }
}
